package com.java.dormitorymanager.mvc.handler;

import com.java.dormitorymanager.pojo.Dorm;
import com.java.dormitorymanager.pojo.Student;
import com.java.dormitorymanager.service.IDormService;
import com.java.dormitorymanager.service.IStudentService;
import com.java.dormitorymanager.service.impl.DormServiceImpl;
import com.java.dormitorymanager.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @program: DormitoryManager
 * @description: 宿舍具体逻辑代码
 * @author: ShuangChenYue
 * @create: 2021-05-17 14:01
 **/
public class DormHandler
{
    IStudentService studentService = new StudentServiceImpl();
    IDormService dormService = new DormServiceImpl();
//=============================================进入宿舍管理页面前所需要准备的数据==========================================
    public void manageDorm(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("通过点击宿舍管理大按钮进入此方法");
            //1.查询dorm表中的man跟woman并存在request中
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            //2.还要准备一个显示宿舍人数的方法。
                /*具体逻辑
                * 2.1
                *
                * */
            //3.请求转发至显示页面显示
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//========================================================添加宿管=====================================================
    public void addDorm(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击添加来到该方法");
            //当用户在输入框中输入信息并点击添加后，便会转到本方法中
            //在此获取用户输入的值，并封装在Dorm对象中，调取dormService.addDorm(Dorm dorm)向数据库中插入
            String dornum = req.getParameter("dornum");
            String type = req.getParameter("type");
            Dorm dorm = new Dorm();
            dorm.setDornum(dornum);
            dorm.setType(type);
            dormService.addDorm(dorm);
            //插入完成后，请求转发至管理宿舍界面，在此之前需要重新查找man和woman的宿舍，用于界面显示
            //重新查询而后转发
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//================================================点击分配学生后来此======================================================
    public void toAddMSToDorm(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("在宿舍管理界面点击添加学生后来到此处");
            //当点击添加按钮后，会带着一个dornum来到此，我们首先获取该dornum
            String dormNumber = req.getParameter("dormNumber");
            //而后将其存在req中，以便请求转发后用
            req.setAttribute("dormNumber",dormNumber);
            //而后调用studentService.findAllMaleStudentsWithoutDorm()返回一个list集合,同样将其存在req里，方便请求转发给下个页面用
            List<Student> allMaleStudentsWithoutDormList = studentService.findAllMaleStudentsWithoutDorm();
            req.setAttribute("allMaleStudentsWithoutDormList",allMaleStudentsWithoutDormList);
            //查询完毕后请求转发给下个页面
            req.getRequestDispatcher("/AddMSToDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//==================================================点击对号后来此========================================================
    public void addMSToDorm(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击对号后进入此方法");
            //点击对号后，会把上个页面获取到的dormNumber和相对应的学生id一起转发过来，我们在此获取
            String dormNumber = req.getParameter("dormNumber");
            String studentId = req.getParameter("id");
            int id = Integer.parseInt(studentId);
            //拿到dormNumber和学生id后，我们调取dormService.addMaleStudentToDorm(String dornumber,int id)
            dormService.addMaleStudentToDorm(dormNumber,id);
            //而后在此处，调取dormService.findAllMSBydormNumber。通过传过来的宿舍编号，查找所有该宿舍编号的学生，返回集合
            List<Student> allMSByDormNumberList = dormService.findAllMSByDormNumber(dormNumber);
            //我们找到了所有的该宿舍编号的学生，并返回了一个集合，获得该集合的size
            int amount = allMSByDormNumberList.size();
            //而后调取通过dormNumber向dorm表中插入amount的数值
            dormService.addAmount(amount,dormNumber);
            //重新查找男女学生后，再转发进管理界面
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===============================================显示宿舍里的人数详情=============================================================
    public void showDormDetail(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击详情按钮进入该方法");
            //点击详情后会带着一个dornum来此，获取该dornum
            String dornum = req.getParameter("dornum");
            //调用dormService.findAllStudentByDornum(String dornum)
            req.setAttribute("dornum",dornum);
            List<Student> allStudentsByDornumList = dormService.findAllStudentsByDornum(dornum);
            req.setAttribute("allStudentsByDornumList",allStudentsByDornumList);
            //获得之后转发至显示界面展示
            req.getRequestDispatcher("/ViewDormDetail").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===================================================从宿舍中删除男学生===================================================
    public void deleteMSFromDorm(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击删除按钮后执行此方法");
            //点击删除按钮后，会带着一个学生的id来到此处，我们在此获取该id
            String studentId = req.getParameter("id");
            //将其强转
            int id = Integer.parseInt(studentId);
            //调用dormService.deleteMSFromDormById
            //删除的基本逻辑：根据传入的学生id，将该id学生的宿舍值更新为null即可
            dormService.deleteStudentFromDormById(id);
            //重新查询而后转发
            //而后重新查询宿舍中的人数，转发至显示界面
            String dornum = req.getParameter("dornum");
            List<Student> allStudentsByDornumList = dormService.findAllStudentsByDornum(dornum);
            int amount = allStudentsByDornumList.size();
            dormService.addAmount(amount,dornum);
            //再重新查询宿舍
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================给宿舍打分=======================================================
    public void addScoreToDorm(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击打分首先进入该方法处理数据");
            //点击打分按钮后，会带着一个dornum来此，在此获取该dornum
            String dornum = req.getParameter("dornum");
            req.setAttribute("dornum",dornum);
            String dormScore = req.getParameter("score");
            int score = Integer.parseInt(dormScore);
            req.setAttribute("score",score);
            //然后请求转发至打分显示界面。
            req.getRequestDispatcher("/ViewAddScoreToDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================================================================================
    public void updateScoreAfterAdd(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            //当打分界面用户点击OK后，便会带着值来此，我们在此获取
            String dornum = req.getParameter("dornum");
            String dormScore = req.getParameter("score");
            int score = Integer.parseInt(dormScore);
            System.out.println(score);
            //调用dormService.updateScoreByDornum(String dornum,int score)方法进行更新分数
            dormService.updateScoreByDornum(dornum,score);
            //重新查找所有宿舍后转发进入宿舍管理界面
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//====================================================删除宿舍===========================================================
    public void deleteDormByDornum(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击删除宿舍按钮执行此方法");
            //会传过来一个dornum，我们接收即可
            String dornum = req.getParameter("dornum");
            System.out.println(dornum);
            //接着调取dormService.deleteDormByDornum(String dornum);
            dormService.deleteDormByDornum(dornum);
            //成功后重新查询所有宿舍后转发至宿舍管理界面
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//==============================================更新宿舍信息前需要准备的数据===============================================
    public void toUpdateDormInfo(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击修改宿舍按钮后来到此方法");
            //这里会传过来一个dornum值，我们将其获取
            String dornum = req.getParameter("dornum");
            //将其存在req中
            req.setAttribute("dornum",dornum);
            String dormId = req.getParameter("id");
            int id = Integer.parseInt(dormId);
            req.setAttribute("id",id);
            //然后转发至修改的显示界面
            req.getRequestDispatcher("/ViewUpdateDormInfo").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=============================================点update按钮后来此方法====================================================
    public void afterUpdateDormInfo(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击update后来此方法");
            //当点击了update后，就提交到了本页面，而后在此处理后进入显示界面
            String dormId = req.getParameter("id");
            int id = Integer.parseInt(dormId);
            String dornum = req.getParameter("dornum");
            //然后调取dormService.updateDormInfoById(int id,String dornum);
            dormService.updateDormInfoById(id,dornum);
            //当修改成功后，该宿舍的宿舍编号发生改变.因为原来的请求会发送一个metaDornum过来，我们获取它
            String metaDornum = req.getParameter("metaDornum");
            //再调去根据metaDornum更新学生dornum的方法
            dormService.updateStudentDornumByMetaDornum(dornum,metaDornum);
            //所有操作完成后，重新查找所有宿舍，然后进入宿舍管理页面。
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================升序排列所有男宿舍================================================
    public void showAllManDormUpWithScore(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("你已来到升序排列男宿舍方法");
            //来此经过升序排列后，获得所有排列好的男宿舍集合，并返回，而后设置进req，进入显示页面
            List<Dorm> allManDormList = dormService.showAllManDormUpWithScore();
            req.setAttribute("allManDormList",allManDormList);
            //还是得重找一下女生宿舍，为了好看
            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================降序排列所有男宿舍================================================
    public void showAllManDormDownWithScore(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("你已来到降序排列男宿舍方法");
            List<Dorm> allManDormList = dormService.showAllManDormDownWithScore();
            req.setAttribute("allManDormList",allManDormList);
            //还是得重找一下女生宿舍，为了好看
            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//====================================================默认排序所有男生宿舍================================================
    public void showAllManDormDefault(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("你已来到默认排列男生宿舍方法");
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//================================================去添加女同学至指定的女宿舍===============================================
    public void toAddWSToDorm(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击添加女同学来到该页面");
            //上个请求会带着一个宿舍编号来到此处，我们先获取它，并将其设置到req里，下个页面会用到
            String dormNumber = (String) req.getParameter("dormNumber");
            req.setAttribute("dormNumber",dormNumber);
            //我们去找到所有没被分配宿舍的女同学，并返回集合
            List<Student> allFemaleStudentsWithoutDormList = studentService.findAllFemaleStudentsWithoutDorm();
            req.setAttribute("allFemaleStudentsWithoutDormList",allFemaleStudentsWithoutDormList);
            //转发至显示界面
            req.getRequestDispatcher("/ViewAddFSToDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//==================================================点击对号后来此========================================================
    public void addFSToDorm(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击对号后来此");
            //点击对号后，会传过来一个dormNumber和对应的女学生id，先将二者获取
            String dormNumber = (String) req.getParameter("dormNumber");
            String femaleStudentId = req.getParameter("id");
            int id = Integer.parseInt(femaleStudentId);
            //然后调用通过id将此id学生的dornum更新成dormNumber
            dormService.addFemaleStudentToDorm(dormNumber,id);
            //添加完成后，根据dormNumber查找所有该dornum的对象，并返回集合
            //提示：dormService.findAllFSByDormNumber(dormNumber);
            List<Student> allFSByDormNumberList = dormService.findAllStudentsByDornum(dormNumber);
            int amount = allFSByDormNumberList.size();
            dormService.addAmount(amount,dormNumber);
            //重新查找男女学生后，再转发进管理界面
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//==================================================升序显示女生宿舍=======================================================
    public void showAllWomanDormUpWithScore(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("你已来到升序排列女生分数方法");
            List<Dorm> allWomanDormList = dormService.showAllWomanDormUpWithScore();
            req.setAttribute("allWomanDormList",allWomanDormList);

            //再查一下男生
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);
            //转发进入管理界面
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//==================================================升序显示女生宿舍=====================================================
    public void showAllWomanDormDownWithScore(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("你已来到降序排列女生分数方法");
            List<Dorm> allWomanDormList = dormService.showAllWomanDormDownWithScore();
            req.setAttribute("allWomanDormList",allWomanDormList);

            //再查一下男生
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);
            //转发进入管理界面
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//================================================默认序列显示女生宿舍====================================================
    public void showAllWomanDormDefault(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("你已来到默认排序女生分数方法");
            List<Dorm> allManDormList = dormService.findAllManDorm();
            req.setAttribute("allManDormList",allManDormList);

            List<Dorm> allWomanDormList = dormService.findAllWomanDorm();
            req.setAttribute("allWomanDormList",allWomanDormList);
            req.getRequestDispatcher("/ViewManageDorm").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=================================================按照宿舍编号查询宿舍===================================================
    public void findDormByDornum(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("你已来到根据宿舍编号查询宿舍的方法");
            //当点击查询后，会带着一个dornum来此，我们将其获取
            String dornum = req.getParameter("dornum");
            //再调用根据dornum查询宿舍的方法
            Dorm dorm = dormService.findDormByDornum(dornum);
            req.setAttribute("dorm",dorm);
            //而后转发至显示页面get显示
            req.getRequestDispatcher("/ViewDormWithSearch").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
